import {
  BASE_URL
} from '../../../http/config';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    excel: [],
    // gender: [],
    manner: [],
    excelNum: 0,
    array: [],
    index: 0,
    city: [
      '河南省',
      '郑州市'
    ]
  },
  // 事件处理
  // 擅长
  onButtonTap(event) {
    // console.log(event.target.dataset.value);
    let num = this.data.excelNum
    const list = this.data.excel
    const index = Number(event.target.dataset.value)
    if (index == 0) {
      if (list[index].isShow) {
        for (let i = 0; i < list.length; i++) {
          list[i].isShow = false
        }
        this.setData({
          excelNum: 0,
          excel: list
        })
      } else {
        for (let i = 1; i < list.length; i++) {
          list[i].isShow = false
        }
        list[0].isShow = true
        this.setData({
          excel: list,
          excelNum: 0
        })
      }
      return;
    }
    if (!list[index].isShow) {
      list[0].isShow = false
      if (num < 3) {
        list[index].isShow = true
        num++
        this.setData({
          excel: list,
          excelNum: num
        })
      } else {
        wx.showToast({
          title: '最多选择三项',
          icon: 'error'
        })
      }
    } else {
      num--
      list[index].isShow = false
      this.setData({
        excelNum: num,
        excel: list
      })
    }
  },
  // 咨询师性别
  // onGender(event) {

  //   const index = Number(event.target.dataset.value)
  //   const list = this.data.gender
  //   console.log(index,list[index]);
  //   for (let i = 0; i < list.length; i++) {
  //     list[i].isShow = false
  //   }
  //   list[index].isShow = true
  //   this.setData({
  //     gender: list
  //   })
  // },
  bindPickerChange: function (e) {
    console.log(e);
    // detail.value
    this.setData({
      index: e.detail.value
    })
  },
  onPickerChange(e) {
    console.log(e.detail.value);

    this.setData({
      city: e.detail.value
    })
  },

  // 咨询方式
  onManner(event) {
    const index = Number(event.target.dataset.value)
    const list = this.data.manner
    // console.log(index,list[index]);
    for (let i = 0; i < list.length; i++) {
      list[i].isShow = false
    }
    list[index].isShow = true
    this.setData({
      manner: list
    })
  },
  // 提交
  onSubmit() {
    const excelList = this.data.excel // 擅长
    // const genderList = this.data.gender
    const mannerList = this.data.manner // 咨询方式
    const city = this.data.city // 地域
    const index = this.data.index // 
    let e = []
    // let g = 0
    let m = 0
    for (let i = 0; i < excelList.length; i++) {
      if (excelList[i].isShow) {
        e = e.concat(i)
      }
    }
    // for (let i = 0; i < genderList.length; i++) {
    //   if (genderList[i].isShow) {
    //     g = i
    //     break;
    //   }
    // }
    for (let i = 0; i < mannerList.length; i++) {
      if (mannerList[i].isShow) {
        m = i
        break;
      }
    }
    console.log(e, m, city, this.data.array[index]);
    const address = city
    const expertList = []
    if (e[0].id!=0) { // 修改
      for (let i = 0; i < e.length; i++) {
        expertList[i] = this.data.excel[e[i]].id
      }
    } else {
      // 擅长全部  
    }
    // 咨询师等级id
    const consultGrade = this.data.array[index].id
    // 咨询方式 id
    const mannerid = mannerList[m].id



    console.log("擅长id", expertList, "地址", address[1], "等级", consultGrade, "咨询方式", mannerid);
    // 构造 URL 查询参数
    const url = `/pages/consult/consult?expertList=${expertList}&address=${address[1]}&consultGrade=${consultGrade}&mannerid=${mannerid}`;

    wx.reLaunch({
      url: url
    });

  },

  // 重置
  onReset() {
    const excelList = this.data.excel
    // const genderList = this.data.gender
    const mannerList = this.data.manner
    const index = [
      '河南省',
      '郑州市'
    ]
    // const city = this.data.city
    for (let i = 0; i < excelList.length; i++) {
      excelList[i].isShow = false
    }
    // for (let i = 0; i < genderList.length; i++) {
    //   genderList[i].isShow = false
    // }
    for (let i = 0; i < mannerList.length; i++) {
      mannerList[i].isShow = false
    }
    excelList[0].isShow = true
    // genderList[0].isShow = true
    mannerList[0].isShow = true

    this.setData({
      excel: excelList,
      // gender: genderList,
      manner: mannerList,
      index: 0,
      city: index,
      excelNum: 0

    })

  },

  // 获取咨询师 擅长领域列表
  getExpert() {
    wx.request({
      url: `${BASE_URL}/expertiseAreas`,
      success: (res) => {
        console.log("擅长领域", res);
        let result = res.data.data;

        // 添加“全部”项到数组开头
        result = [{
            id: 0,
            expertise: "全部",
            isShow: true
          },
          ...result.map(item => ({
            ...item, // 保留原有属性
            isShow: false // 添加新属性 isShow
          }))
        ];

        this.setData({
          excel: result
        });
      }
    })
  },
  // 获取咨询师 等级列表
  getGrade() {
    wx.request({
      url: `${BASE_URL}/consultGrade`,
      success: (res) => {
        console.log("等级", res);
        this.setData({
          array: res.data.data
        })
      }
    })
  },
  // 获取咨询师 咨询方式
  getChargesClass() {
    wx.request({
      url: `${BASE_URL}/consultChargesClassify`,
      success: (res) => {
        console.log("咨询分类", res);
        let result = res.data.data;

        // 为每条数据添加 isShow 属性，并设置为 false
        result = [{
            id: 0,
            classifyname: '全部',
            isShow: true
          },
          ...result.map(item => ({
            ...item,
            isShow: false
          }))
        ]


        this.setData({
          manner: result
        });
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getExpert()
    this.getGrade()
    this.getChargesClass()
  },

})